/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.wxmg.fansMessage.service.imp;

import com.hyyt.shopjsp.basic.service.IBasicActorService;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import com.hyyt.shopjsp.wxmg.basicInfo.pojo.WxGlobalReturnCode;
import com.hyyt.shopjsp.wxmg.fansMessage.dao.WxVideoMessageInfoDAO;
import com.hyyt.shopjsp.wxmg.fansMessage.pojo.WxVideoMessageInfo;
import com.hyyt.shopjsp.wxmg.fansMessage.service.IWxVideoMessageInfoService;
import com.hyyt.shopjsp.wxmg.fansMessage.service.IWxVideoMessageInfoService;
import com.hyyt.shopjsp.wxmg.sendMessage.pojo.WxPublicNumberSendMsgInfo;
import com.hyyt.shopjsp.wxmg.sendMessage.service.IWxPublicNumberSendMsgInfoService;
import com.hyyt.shopjsp.wxmg.util.WxBasicUtil;
import com.hyyt.shopjsp.wxmg.util.WxGlobalReturnCodeUtil;
import com.hyyt.shopjsp.wxmg.util.WxUploadForMidea;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* wx_text_message_info, WX_视频消息信息
 *
 * @author wyc 17-7-23 18:15
 */
@Service
public class WxVideoMessageInfoService implements IWxVideoMessageInfoService {
    private static  final Logger logger = Logger.getLogger(IWxVideoMessageInfoService.class);

    @Resource
    private WxVideoMessageInfoDAO wxVideoMessageInfoDAO;
    @Resource
    private IWxPublicNumberSendMsgInfoService wxPublicNumberSendMsgInfoService;
    /**
     * 保存WX_视频消息信息
     *
     * @param wxVideoMessageInfo WX_视频消息信息
     * @return 保存后的WX_视频消息信息
     */
    @Override
    public void saveWxVideoMessageInfo(WxVideoMessageInfo wxVideoMessageInfo,Map<String,Object> wxPublicNoInfoMap) {
        Integer publicNumberId = (Integer) wxPublicNoInfoMap.get("publicNumberId");
        wxVideoMessageInfo.setPublicNumberId(publicNumberId);
        wxVideoMessageInfo.setPublicNumberName((String) wxPublicNoInfoMap.get("wxName"));
        wxVideoMessageInfo.setReplyFlag(0);
        wxVideoMessageInfo.setCollectFlag(0);
        wxVideoMessageInfo.setCreateTime(new Timestamp(new Date().getTime()));
        //存储用户图片
        String access_token = RedisDataReadUtil.getRedisValue(StringStaticKey.WX_ACCESS_TOKENS);
        String videoUrl = WxUploadForMidea.getMideaByMideaId(access_token, wxVideoMessageInfo.getMediaId());
        wxVideoMessageInfo.setVideoUrl(videoUrl);
        wxVideoMessageInfoDAO.insert(wxVideoMessageInfo);
    }

    /**
     * 更新WX_视频消息信息
     *
     * @param wxVideoMessageInfo WX_视频消息信息
     * @return 更新后的WX_视频消息信息
     */
    @Override
    public void updateWxVideoMessageInfo(WxVideoMessageInfo wxVideoMessageInfo) {
        wxVideoMessageInfoDAO.update(wxVideoMessageInfo);
    }

    /**
     * 删除指定ID的WX_视频消息信息
     *
     * @param id WX_视频消息信息ID
     */
    @Override
    public void deleteWxVideoMessageInfo(Integer id) {
        wxVideoMessageInfoDAO.delete(id);
    }

    /**
     * 根据ID加载一个WX_视频消息信息
     *
     * @param id WX_视频消息信息ID
     * @return WX_视频消息信息
     */
    @Override
    public WxVideoMessageInfo loadWxVideoMessageInfo(Integer id) {
        return wxVideoMessageInfoDAO.load(id);
    }

    /**
     * 查询所有的WX_视频消息信息，通常有最多取30000条记录的限制
     *
     * @return 所有的WX_视频消息信息，通常有最多取30000条记录的限制
     */
    @Override
    public List<WxVideoMessageInfo> queryAllWxVideoMessageInfo() {
        return wxVideoMessageInfoDAO.queryAll();
    }

    /**
     * 查询WX_视频消息信息
     *
     * @param params 查询参数，为key-value形式
     * @return WX_视频消息信息集合
     */
    @Override
    public List<WxVideoMessageInfo> queryWxVideoMessageInfo(Map<String, Object> params) {
        return wxVideoMessageInfoDAO.query(params);
    }

    /**
     * 查询WX_视频消息信息
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return WX_视频消息信息集合
     */
    @Override
    public List<WxVideoMessageInfo> queryWxVideoMessageInfo(Map<String, Object> params, int startRow, int rowSize) {
        return wxVideoMessageInfoDAO.query(params, startRow, rowSize);
    }

    /**
     * 统计WX_视频消息信息
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countWxVideoMessageInfo(Map<String, Object> params) {
        return wxVideoMessageInfoDAO.count(params);
    }

    /**
     * 关联查询视频消息信息数量
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    public int countWxVideoMessageInfoMap(Map<String, Object> params) {
        return wxVideoMessageInfoDAO.count("countWxVideoMessageInfoMap",params);
    }

    /**
     * 关联查询视频消息信息
     * @param params
     * @param startRow
     * @param rowSize
     * @return
     */
    public List<Map<String,Object>> queryWxVideoMessageInfoMap(Map<String, Object> params, int startRow, int rowSize){
        return wxVideoMessageInfoDAO.query("queryWxVideoMessageInfoMap",params, startRow, rowSize);
    }

    /**
     * 回复消息
     * @param wxName
     * @param sendMsgContent
     * @param videoMessageInfo
     * @param publicNoInfo
     * @param access_token
     * @param globalReturnCodeList
     * @return
     */
    public String saveReplyMessage(String wxName, String sendMsgContent, WxVideoMessageInfo videoMessageInfo, Map<String,Object> publicNoInfo, String access_token, List<WxGlobalReturnCode> globalReturnCodeList ){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("touser", videoMessageInfo.getFromUserName());
        params.put("msgtype", "text");
        //定义子map
        Map<String, Object> jsonSonMap = new HashMap<String, Object>();
        jsonSonMap.put("content",sendMsgContent);
        params.put("text", jsonSonMap);
        String strJsonMap= FastJsonUtils.toJSONString(params);
        //发送客服消息
        String strJson= WxBasicUtil.sendMessage(access_token,strJsonMap);
        //回复消息
        String backMessage = WxGlobalReturnCodeUtil.getGlobalReturnCode(strJson,globalReturnCodeList);
        logger.info("******** 微信发送消息返回结果 WxVideoMessageInfoService sendMessage = " + backMessage);
        //判断返回值，1：true 正确 ，2：错误提示消息
        if("true".equals(backMessage)){
            WxPublicNumberSendMsgInfo wXPublicNumberSendMsgInfo = new WxPublicNumberSendMsgInfo();
            wXPublicNumberSendMsgInfo.setUserName(wxName);//保存粉丝用户名称
            wXPublicNumberSendMsgInfo.setFanUserName(videoMessageInfo.getFromUserName());
            wXPublicNumberSendMsgInfo.setMessageInfoId((long)videoMessageInfo.getVideoMessageInfoId());
            wXPublicNumberSendMsgInfo.setPublicNumberName(String.valueOf(publicNoInfo.get("wxName")));
            wXPublicNumberSendMsgInfo.setPublicNumberIdBigint(Long.parseLong(String.valueOf(publicNoInfo.get("publicNumberId"))));
            wXPublicNumberSendMsgInfo.setSendMsgContent(sendMsgContent);
            wXPublicNumberSendMsgInfo.setSendMsgTypeEnumId(1);
            wXPublicNumberSendMsgInfo.setRemark("粉丝视频消息回复");
            wXPublicNumberSendMsgInfo.setSendTime(new Timestamp(System.currentTimeMillis()));
            wxPublicNumberSendMsgInfoService.saveWxPublicNumberSendMsgInfo(wXPublicNumberSendMsgInfo);
            WxVideoMessageInfo videoMessageInfoLoad = wxVideoMessageInfoDAO.load(videoMessageInfo.getVideoMessageInfoId());
            videoMessageInfoLoad.setReplyFlag(1);
            videoMessageInfoLoad.setReplyFlagTime(new Timestamp(System.currentTimeMillis()));
            wxVideoMessageInfoDAO.update(videoMessageInfoLoad);
            return "true";
        }else{
            return backMessage;
        }
    }

}